//
//  SongInfo.m
//  kyMusicPlayer
//
//  Created by 翁科宇 on 2018/5/14.
//  Copyright © 2018年 翁科宇. All rights reserved.
//

#import "SongInfo.h"

@implementation SongInfo
static SongInfo *_sharedManager = nil;

+ (SongInfo *)sharedManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[self alloc] init];
    });
    return _sharedManager;
}

#pragma mark - 获取歌曲url
- (void)getSelectedSong:(NSString *)songID index:(long)index{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *path = [@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.song.play&songid=" stringByAppendingString:songID];
    [manager GET:path parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSDictionary *array = [responseObject objectForKey:@"bitrate"];
            self.file_link = [array objectForKey:@"file_link"];
            self.file_size = [array objectForKey:@"file_size"];
            self.file_duration = [array objectForKey:@"file_duration"];
            self.playSongIndex = index;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"error--%@",error);
    }];
}

#pragma mark - 设置歌曲的所有信息
- (void)setSongInfo:(HotSongInfo *)info{
    self.title = info.title;
    self.author = info.author;
    self.album_title = info.author;
    self.lrclink = info.lrclink;
    
    NSString *path = info.pic_small;
    if (path) {
        NSURL *url = [NSURL URLWithString:path];
        @try{
            NSData *imageData = [NSData dataWithContentsOfURL:url];
            UIImage *albumArt = [UIImage imageWithData:imageData];
            if (albumArt) {
                self.pic_small = albumArt;
            }else{
                self.pic_small = [UIImage imageNamed:@"album_default"];
            }
        }@catch (NSException *exception){
            NSLog(@"下载图片出错!");
        }
    }
    else{
        self.pic_small = [UIImage imageNamed:@"album_default"];
    }
    
    
    path = info.pic_big;
    if (path) {
        NSURL *url = [NSURL URLWithString:path];
        @try{
            NSData *imageData = [NSData dataWithContentsOfURL:url];
            UIImage *albumArt = [UIImage imageWithData:imageData];
            if (albumArt) {
                self.pic_big = albumArt;
            }
            else{
                self.pic_big = [UIImage imageNamed:@"album_default"];
            }
            
        }@catch(NSException *exception){
            NSLog(@"下载图片出错!");
            
        }
    }
    else{
        self.pic_big = [UIImage imageNamed:@"album_default"];
    }
    
    
    path = info.lrclink;
    if (![path isEqualToString:@""] && path != nil) {
        NSURL *url = [NSURL URLWithString:path];
        @try{
            NSData *lrcData = [NSData dataWithContentsOfURL:url];
            NSString *lrcStr = [[NSString alloc] initWithData:lrcData encoding:NSUTF8StringEncoding];
            if (![lrcStr isEqualToString:@""] && lrcStr != nil) {
                self.lrcString = lrcStr;
                self.isLrcExistFlg = YES;
            }else{
                NSLog(@"获取歌词出错!");
                self.isLrcExistFlg = NO;
            }
        }@catch(NSException *exception){
            NSLog(@"获取歌词出错!");
            self.isLrcExistFlg = NO;
        }
    }
    else{
        NSLog(@"歌词不存在!");
        self.isLrcExistFlg = NO;
    }
    
}

#pragma mark - int转NSString
- (NSString *)intToString:(NSInteger)needTransformInteger{
    //实现00：00这种格式播放时间
    NSInteger wholeTime = needTransformInteger;
    NSInteger min = wholeTime / 60;
    NSInteger sec = wholeTime % 60;
    NSString *str = [NSString stringWithFormat:@"%02ld:%02ld", min, sec];
    return str;
}

#pragma mark - NSString转integer
- (NSInteger)stringToInt:(NSString *)timeString{
    NSArray *strTemp = [timeString componentsSeparatedByString:@":"];
    NSInteger time = [strTemp.firstObject integerValue] * 60 + [strTemp.lastObject integerValue];
    return time;
}
@end
